﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WB.Entity;
using WB.Entity.Helper;
using WB.Library;
using Tongiao.Entity;
using Tongiao.ManagerBase;
using Tongiao.Manager;

public partial class admin_Products_AddEditPro : System.Web.UI.UserControl
{

    public Guid IdPro
    {
        get { return FGuid.ToGuid(hidInfoId.Value); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            hidInfoId.Value = Request["id"];
            if (hidInfoId.Value == "")
                btnUpdate.Text = "Thêm mới";
            else
                btnUpdate.Text = "Cập nhật";
            LoadCategory();
            LoadData();

        }
    }

    void LoadCategory()
    {
        string catType = "PROD";
        DataTable dtCat = Tg_CategoryManager.CreateInstant().getAllCategoryListByCatType(catType);
        if (dtCat.Rows.Count > 0)
        {
            ddlCategory.DataSource = dtCat;
            ddlCategory.DataTextField = "CatNameChild";
            ddlCategory.DataValueField = "IdChild";
            ddlCategory.DataGroupField = "CatName";
            ddlCategory.DataBind();
        }
    }

    void Insert()
    {
        Tg_ProductsEntity objPro = new Tg_ProductsEntity();
        objPro.CategoryId = new Guid(ddlCategory.SelectedValue);
        objPro.ProductName = txtProductName.Text;
        objPro.Title = txtInfor.Text;
        objPro.Specification = txtContent.Value.ToString();
        objPro.Price = Double.Parse(txtPrice.Text);
        objPro.ProductCode = txtCode.Text;

        if (filePhoto.HasFile)
            objPro.Photo = FileUploadControl.FullPath(filePhoto, txtCode.Text, ProductsField.Photo.MaxLength, EnumsFile.Files);
        objPro.CreatedBy = Profile.UserName;
        objPro.CreatedDate = DateTime.Now;

        Tg_ProductsManager.CreateInstant().Insert(objPro);
        Response.Redirect("?control=PProAddEdit&Id=" + IdPro.ToString());
        CustomValidator1.ErrorMessage = "Đã thêm dữ liệu thành công.";
        CustomValidator1.IsValid = false;

    }

    void Update()
    {
        Tg_ProductsEntity objPro = new Tg_ProductsEntity();
        objPro.Id = IdPro;
        objPro.ProductName = txtProductName.Text;
        objPro.ProductCode = txtCode.Text;
        objPro.Title = txtInfor.Text;
        objPro.Specification = txtContent.Value.ToString();
        objPro.Price = Double.Parse(txtPrice.Text);
        objPro.CategoryId = new Guid(ddlCategory.SelectedValue);
       
        if (filePhoto.HasFile)
            objPro.Photo = FileUploadControl.FullPath(filePhoto, txtCode.Text, ProductsField.Photo.MaxLength, EnumsFile.Files);
        Tg_ProductsManager.CreateInstant().Update(objPro);
        Response.Redirect("?control=PProAddEdit&id=" + IdPro.ToString());
        CustomValidator1.ErrorMessage = "Đã thêm dữ liệu thành công.";
        CustomValidator1.IsValid = false;

    }

    void LoadData()
    {
        if (IdPro != Guid.Empty)
        {

            DataTable tbl = Tg_ProductsManager.CreateInstant().SelectById(IdPro);
            if (tbl.Rows.Count > 0)
            {

                hidInfoId.Value = tbl.Rows[0]["Id"].ToString();
                txtProductName.Text = tbl.Rows[0]["ProductName"].ToString();
                txtContent.Value = tbl.Rows[0]["Specification"].ToString();
                txtInfor.Text = tbl.Rows[0]["Title"].ToString();
                txtPrice.Text = tbl.Rows[0]["Price"].ToString();
                txtCode.Text = tbl.Rows[0]["ProductCode"].ToString();
                if (tbl.Rows[0]["CategoryId"].ToString() != "")
                {
                    ddlCategory.SelectedValue = tbl.Rows[0]["CategoryId"].ToString();
                }
                if (!string.IsNullOrEmpty(tbl.Rows[0]["Photo"].ToString()))
                {
                    imgPhoto.Visible = true;
                    imgPhoto.ImageUrl = tbl.Rows[0]["Photo"].ToString();
                }
            }
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (hidInfoId.Value == "")
        {

            Insert();
            btnUpdate.Text = "Cập nhật";


        }
        else
        {
            btnUpdate.Text = "Cập nhật";
            Update();
            LoadData();
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("?control=PProList");
    }
}